package com.shanghaionstar.nav.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.mag.dealer.PreferredDealerEntity;
import com.shanghaionstar.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 周边经销商列表适配器：
 * Created by Yonglang Hong on 2016/7/21.
 */
public class AroundDealerListAdapter extends CommonAdapter<PreferredDealerEntity> {

    /**周边经销商列表**/
//    private List<PreferredDealerEntity> mAroundDealerList;

    public List<PreferredDealerEntity> getAroundDealerList() {
        if(datas == null) {
            datas =  new ArrayList<PreferredDealerEntity>();
        }
        return datas;
    }

    public AroundDealerListAdapter(List<PreferredDealerEntity> datas, Context context) {
        super(datas, context);
    }

    public AroundDealerListAdapter(Context context) {
        super(new ArrayList<PreferredDealerEntity>(), context);
    }

    @Override
    public void getView(int position, CommonViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder)viewHolder;
        holder.bindData(datas.get(position));
    }

    @Override
    public int layoutId() {
        return R.layout.nav_around_dealer_item;
    }

    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    public void add(List<PreferredDealerEntity> dealerEntityList){
        if (dealerEntityList == null)
            return;
        getAroundDealerList().addAll(dealerEntityList);
        notifyDataSetChanged();
    }

    private class ViewHolder extends CommonViewHolder {
        @FindViewById(R.id.dealer_name)
        TextView dealer_name;
        @FindViewById(R.id.dealer_distance)
        TextView dealer_distance;
        @FindViewById(R.id.dealer_address)
        TextView dealer_address;
        @FindViewById(R.id.dealer_call)
        TextView dealer_call;

        public void bindData(PreferredDealerEntity dealerEntity){
            dealer_name.setText(TextUtils.isEmpty(dealerEntity.getDealerName())?"":dealerEntity.getDealerName());
            dealer_distance.setText(dealerEntity.getDistanceWithUnit());
            dealer_address.setText(TextUtils.isEmpty(dealerEntity.getAddress())?"":dealerEntity.getAddress());
            dealer_call.setText(TextUtils.isEmpty(dealerEntity.getTelephone())?"":dealerEntity.getTelephone());
        }
    }

}
